#!/bin/sh

INSTALL_PATH=/usr/local
COMPONENT_NAME=vrcm
INFRA_COMPONENT_NAME=ivsi
CPU_NUM=`grep -P '^core id\t' /proc/cpuinfo | sort -u | wc -l`

module_build()
{
    local module_name=$1
    local config_args=$2
    local target=$3
    local pre_hook=$4
    local ret=0
    local flag_filename=.vrcm_configured
    echo "======================"
    echo "Building $module_name"
    echo "======================"
    if [ -d $module_name ]; then
        cd $module_name
        if [ "$target" = "clean" ]; then
            make $target -j${CPU_NUM}
            rm -f $flag_filename
        else
            if [ -f $flag_filename ];then
                make $target -j${CPU_NUM}
                ret=$?
            else
                $pre_hook
                if [ -x configure ];then
                    bash -c "./configure $config_args" && make -j${CPU_NUM} && make $target -j${CPU_NUM}
                else
                    make -j${CPU_NUM} && make $target -j${CPU_NUM}
                fi
                ret=$?
                if [ "$ret" = "0" ]; then
                    touch $flag_filename
                fi
            fi
        fi
        cd -
    else
        echo "$module_name doesn't exist"
        ret=1
    fi

    return $ret
}

module_sync()
{
    local module_name=$1
    local target=$2

    rsync -a --exclude ".svn" --exclude "*~" --exclude ".git" --exclude ".DS_Store" $module_name $target
    ret=$?

    return $ret
}

python_package()
{
    local src_dir=$1
    local py_path=$2
    local home_path=$3
    local script_path=$4
    local org_py_path=$PYTHONPATH

    cd $src_dir
    mkdir -p $py_path
    export PYTHONPATH=$PYTHONPATH:$py_path
    python setup.py install --home=$home_path --install-scripts=$script_path
    ret=$?
    export PYTHONPATH=$org_py_path
    cd -
    return $ret
}

centos_version="6"
build_version="4.2.2"
target="install"

while getopts "cv:n:" optname
do
    case "$optname" in
    "c")
        target="clean"
        ;;
    "v")
        centos_version="$OPTARG"
        echo "Build Version $OPTARG"
        ;;
    "n")
        build_version="$OPTARG"
        echo "build_version $OPTARG"
        ;;
    *)
        # Should not occur
        echo "Unknown error while processing options"
        ;;
    esac
    echo "OPTIND is now $OPTIND"
done

MAJOR_NUM=`echo ${build_version} | awk -F "." '{printf $1}'`
MINOR_NUM=`echo ${build_version} | awk -F "." '{printf $2}'`
REVISION_NUM=`echo ${build_version} | awk -F "." '{printf $3}'`
#BUILD_NUM=`svnversion -n . |awk -F: '{printf $NF}'`
BUILD_NUM=`git rev-list HEAD | wc -l`".el$centos_version"
BUILD_TIME=`date`
RELEASE_NUM=$MAJOR_NUM.$MINOR_NUM.$REVISION_NUM
RELEASE_VER=$MAJOR_NUM.$MINOR_NUM.$REVISION_NUM-b$BUILD_NUM
install_dir=/opt/$COMPONENT_NAME-$RELEASE_NUM
target_vrcm="install DESTDIR=$install_dir"

find . -name "*~" -delete
rm -rf $install_dir

if [ "$target" = "clean" ]; then
  git clean -d -x -f
  echo "Cleanup finished"
  exit 0
fi

mkdir -p $install_dir$INSTALL_PATH/$COMPONENT_NAME/conf

module_sync conf/logstash-forwarder.conf.d $install_dir/etc
if [ "$?" != "0" ]; then 
  exit 1
fi

module_sync conf/profile.d $install_dir/etc
if [ "$?" != "0" ]; then 
  exit 1
fi

module_sync conf/logrotate.d $install_dir/etc
if [ "$?" != "0" ]; then 
  exit 1
fi

mkdir -p $install_dir$INSTALL_PATH/$COMPONENT_NAME/proxy/conf
module_sync conf/certs $install_dir$INSTALL_PATH/$COMPONENT_NAME/proxy/conf
if [ "$?" != "0" ]; then 
  exit 1
fi

mkdir -p $install_dir$INSTALL_PATH/$COMPONENT_NAME/conf/db
module_sync src/vrcm/version/mysql/ $install_dir$INSTALL_PATH/$COMPONENT_NAME/conf/db
if [ "$?" != "0" ]; then
  exit 1
fi

module_sync src/binary/ $install_dir$INSTALL_PATH/$COMPONENT_NAME
if [ "$?" != "0" ]; then
  exit 1
fi

module_sync infra/consul-template/template $install_dir$INSTALL_PATH/$COMPONENT_NAME
if [ "$?" != "0" ]; then
  exit 1
fi

mkdir -p $install_dir$INSTALL_PATH/$COMPONENT_NAME/conf

module_sync conf/vrcm-startup.json.sample $install_dir/$INSTALL_PATH/$COMPONENT_NAME/conf
if [ "$?" != "0" ]; then 
  exit 1
fi

module_sync conf/mime.types $install_dir/$INSTALL_PATH/$COMPONENT_NAME/conf
if [ "$?" != "0" ]; then 
  exit 1
fi

module_sync script $install_dir/usr/local/$COMPONENT_NAME
if [ "$?" != "0" ]; then
  exit 1
fi

module_sync tools $install_dir/usr/local/$COMPONENT_NAME
if [ "$?" != "0" ]; then
  exit 1
fi

mkdir -p $install_dir$INSTALL_PATH/$INFRA_COMPONENT_NAME/conf/consul-template
if [ "$centos_version" = "6" ]
then
    module_sync conf/init $install_dir/etc
    if [ "$?" != "0" ]; then 
      exit 1
    fi

    module_sync infra/consul-template/conf/consul-template/vrcm_service_6.conf $install_dir$INSTALL_PATH/$INFRA_COMPONENT_NAME/conf/consul-template/vrcm_service.conf
    if [ "$?" != "0" ]; then 
      exit 1
    fi
fi

if [ "$centos_version" = "7" ]
then
    module_sync infra/consul-template/conf/consul-template/vrcm_service_7.conf $install_dir$INSTALL_PATH/$INFRA_COMPONENT_NAME/conf/consul-template/vrcm_service.conf
    if [ "$?" != "0" ]; then 
      exit 1
    fi

    module_sync conf/systemd $install_dir/etc
    if [ "$?" != "0" ]; then 
      exit 1
    fi

    module_sync conf/sysconfig $install_dir/etc
    if [ "$?" != "0" ]; then 
      exit 1
    fi
fi

mkdir -p $install_dir$INSTALL_PATH/$COMPONENT_NAME/bin/
SHOW_VER_PATH=$install_dir$INSTALL_PATH/$COMPONENT_NAME/bin/show_${COMPONENT_NAME}_ver.sh
PACKAGE_INFO_PATH=$install_dir$INSTALL_PATH/$COMPONENT_NAME/conf/package-info.json

printf "#!/bin/sh

printf \"Video Solar VRC Management Software (vrcm)
Copyright (c) 2014-2022 by VideoSolar, Inc.
Version: $RELEASE_VER

Compiled at $BUILD_TIME by vsbuild\n\"
" > $SHOW_VER_PATH

printf "{
  \"name\": \"$COMPONENT_NAME\",
  \"version\": \"$RELEASE_VER\"
}
" > $PACKAGE_INFO_PATH

chmod +x $SHOW_VER_PATH

find $install_dir -name ".svn" -type d -o -name ".git" -type d -o -name "*~" |xargs rm -rf

rpmbuild_root=/opt/rpmbuild

mkdir -p $rpmbuild_root/{RPMS,SRPMS,BUILD,SOURCES,SPECS,tmp}
cat <<EOF >~/.rpmmacros
%_topdir   $rpmbuild_root
%_tmppath  %{_topdir}/tmp
EOF
cp rpm/$COMPONENT_NAME"_$centos_version".spec $rpmbuild_root/SPECS
cd /opt ; 
#chmod 555 /opt/$COMPONENT_NAME-$RELEASE_NUM/usr/bin
#chmod 555 /opt/$COMPONENT_NAME-$RELEASE_NUM/usr/sbin
#chmod 555 /opt/$COMPONENT_NAME-$RELEASE_NUM/usr/lib
tar czvf $rpmbuild_root/SOURCES/$COMPONENT_NAME-$RELEASE_NUM.tar.gz $COMPONENT_NAME-$RELEASE_NUM/; cd -;
rpmbuild -bb --define "RELEASE_NUM $RELEASE_NUM" --define "BUILD_NUM $BUILD_NUM" --define "COMPONENT_NAME $COMPONENT_NAME" --clean $rpmbuild_root/SPECS/$COMPONENT_NAME"_$centos_version".spec
